Ext.define('AmpleHarvest.objects.hierarchy.hierarchy',
		{
			extend			: 'Ext.tree.Panel',
			alias			: 'widget.ampleharvest-objects-hierarchy-hierarchy',
			title			: 'Hierarchy',
			iconCls			: 'toolbar-article-navigation-files',
			rootVisible		: false,

			// PASSED MEMBER VARIABLES
			// REQUIRED
			m_str_data_source	: false,
			
			// OPTIONAL
			m_obj_params		: {},
			m_str_keyID			: '',
			columns				: [{
					xtype 	: 'treecolumn',
					text	: 'File',
					flex	: 8,
					sortable: true,
					scope	: this,
					dataIndex: 'text',
					renderer: function (value, metadata, record, colIndex, view) {
						var obj_panel = m_obj_AmpleHarvest_ViewPort.down('ampleharvest-objects-hierarchy-hierarchy');
						if (!record.raw.leaf) {
				            obj_panel.columns[1].items[0].icon = '';
				        } else {
				        	obj_panel.columns[1].items[0].icon = '../common/img/icons/fam/file_open.gif';
				        }
						if (record.raw.download == null) {
				            obj_panel.columns[2].items[0].icon = '';
				        } else {
				        	obj_panel.columns[2].items[0].icon = '../common/img/icons/fam/download.gif';
				        }
						return(value);
				    },
				},
				{
					text	: 'Open',
					xtype	: 'actioncolumn',
					tooltip	: 'Download File',
					align	: 'center',
					flex	: 1,
					handler : function(grid, rowIndex, colIndex, actionItem, event, record, row)
					{
						if(record.raw.leaf)
						{
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_OPEN_FLATFILE,
										{
											m_str_id 	: record.raw.id,
											m_str_urn 	: record.raw.urn
										}
									);
						}
					}
				},
				{
					text	: 'Download',
					xtype	: 'actioncolumn',
					tooltip	: 'View file content',
					align	: 'center',
					flex	: 2,
					handler : function(grid, rowIndex, colIndex, actionItem, event, record, row)
					{
						if(record.raw.download != null)
						{
							var AHcaller = this.up('ampleharvest-objects-hierarchy-hierarchy').AHcaller;
							AHcaller.fireEvent('downloadfile', record.raw.download, this);
						}
					}
				}],
			
			listeners		:
			{
				dataResponse	: function(obj_response)
				{
					if(Ext.isObject(obj_response.data))
					{
						if(obj_response.data.parent)
						{
							this.AHcaller.fireEvent('genParentLink', obj_response.data.parent.urn, this);
						}
						
						if(obj_response.data.root.download)
						{
							this.AHcaller.fireEvent('genDownloadLink', obj_response.data.root.download.urn, this);
						}
						
						if(Ext.isArray(obj_response.data.root.childNodes))
						{
							var treeData = this.convertTOtree(this.getStore().getRootNode(), obj_response.data.root.childNodes);
							this.AHcaller.fireEvent('objectResponse', this.m_str_keyID, this);
						} else {
							this.destroy();
						}
					}
				},
				
				beforedestroy	: function(obj_self)
				{
					obj_self.m_obj_util.destroy();
				}
			},
			
			initComponent	: function()
			{
				if(this.m_str_data_source)
				{
					this.m_obj_util = Ext.create('AmpleHarvest.util.utility');
					this.m_obj_util.AHcaller = this;

					this.store = Ext.create('Ext.data.TreeStore',
							{
								root	:
								{
									expanded	: true,
									children	: []
								}
							});
					
					this.m_obj_util.getData(this.m_str_data_source, this.m_obj_params, {},'GET', {}, {});
				} else {
					this.html = "This object requires a resource path as m_str_data_source";
				}
					
				this.callParent();
			},
			
			convertTOtree	: function(obj_node, obj_data)
			{
				for (var var_i in obj_data)
				{				
					var str_dl_urn = Ext.isObject(obj_data[var_i]['download']) ? obj_data[var_i]['download']['urn'] : null;
					var str_urn = Ext.isObject(obj_data[var_i]['self']) ? obj_data[var_i]['self']['urn'] : null;
					
					if(obj_data[var_i]['children'] != null)
					{
						
						
						var nodeLink = obj_node.appendChild({
							text		: obj_data[var_i]['text'],
							expanded	: true,
							children	: [],
							download	: str_dl_urn,
							id			: obj_data[var_i]['id']
						});
						
						

						this.convertTOtree(nodeLink, obj_data[var_i]['children']['childNodes'])

					} else {
						var str_type = obj_data[var_i]['download'] ? obj_data[var_i]['download']['type'] : 'page';
						obj_node.appendChild({
							text		: obj_data[var_i]['text'],
							leaf		: true,
							iconCls		: str_type,
							download	: str_dl_urn,
							urn			: str_urn,
							id			: obj_data[var_i]['id']
						});
						
						
					}
					
				}
			}
		});